package com.gdpu.project.util.xml;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class MSColumn3DXML {

	public Boolean createXml(Map<String, String[]> map, String[] years,
			String[] colors, String fileName) {
		Boolean flag = false;
		try {
			/** 定义创建时间 */
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String date = s.format(new Date());

			/** 建立document对象 */
			Document document = DocumentHelper.createDocument();

			/** 建立XML文档的根graph */
			Element graphElement = document.addElement("graph");

			/** 加入一行注释 */
			graphElement.addComment("This is a new XML file,创建日期：" + date);

			/** 创建根节点属性 */
			graphElement.addAttribute("xaxisname", "年份");
			graphElement.addAttribute("caption", "近年几年各类项目的变化情况");
			graphElement.addAttribute("subcaption", "5年");
			// 设定小数的位数
			graphElement.addAttribute("decimalPrecision", "0");
			graphElement.addAttribute("yAxisName", "amount");
			graphElement.addAttribute("hovercapbg", "DEDEBE");
			graphElement.addAttribute("hovercapborder", "889E6D");
			graphElement.addAttribute("rotateNames", "0");
			graphElement.addAttribute("yAxisMaxValue", "100");
			graphElement.addAttribute("numdivlines", "9");
			graphElement.addAttribute("divLineColor", "CCCCCC");

			/** 创建categories节点 */
			Element categoriesElement = graphElement.addElement("categories");
			categoriesElement.addAttribute("font", "Arial");
			categoriesElement.addAttribute("fontSize", "11");
			categoriesElement.addAttribute("fontColor", "000000");
			/** 创建category节点 */
			for (int i = 0; i < years.length; i++) {
				Element categoryElement = categoriesElement
						.addElement("category");
				categoryElement.addAttribute("name", years[i]);
				categoryElement.addAttribute("hoverText", years[i]);
			}

			/** 创建dataset节点 */
			Set<String> keys = map.keySet();
			Iterator<String> it = keys.iterator();
			int i = 0;
			while (it.hasNext()) {
				String name = it.next();
				String[] setElements = map.get(name);
				Element datasetElement = graphElement.addElement("dataset");
				datasetElement.addAttribute("seriesname", name);
				datasetElement.addAttribute("color", colors[i++]);
				for (int j = 0; j < setElements.length; j++) {
					/** 创建set节点 */
					Element setElement = datasetElement.addElement("set");
					setElement.addAttribute("value", setElements[j]);
				}
			}

			/** 将document中的内容写入文件中 */
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			XMLWriter writer = new XMLWriter(new OutputStreamWriter(
					new FileOutputStream(fileName), "UTF-8"));
			writer.write(document);
			writer.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("生成XML文件失败！");
		}
		return flag;
	}
}
